package com.atguigu.gmall.order.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.order.model.OrderInfo;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * title:
 * author: Gyh
 * date:2023-05-07 14:44
 * Description:
 */
@RestController
@RequestMapping("/api/order/auth")
public class orderController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 提交保存订单
     * @param orderInfo
     * @return
     */
    @PostMapping("/submitOrder")
    public Result submitOrder(HttpServletRequest request, @RequestBody OrderInfo orderInfo, @RequestParam("tradeNo") String tradeNo) {
        String userId = AuthContextHolder.getUserId(request);
        orderInfo.setUserId(Long.valueOf(userId));
        Long orderId = orderInfoService.submitOrder(orderInfo, tradeNo);
        return Result.ok(orderId);
    }

    /**
     * 查询我的订单列表
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result getOrderList(HttpServletRequest request,
                               @PathVariable("page") Long page,
                               @PathVariable("limit") Long limit) {
        Page<OrderInfo> infoPage = new Page<>(page, limit);
        //获取当前用户ID
        String userId = AuthContextHolder.getUserId(request);
        infoPage = orderInfoService.getOrderList(infoPage, userId);
        return Result.ok(infoPage);
    }

}
